# po模型 page object对象
from selenium import  webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from config.conf import BASE_URL
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
class BasePage:
#创建初始化方法，生成一个浏览器对象
      def __init__(self,driver=None):
          if driver is None:
              self.driver = webdriver.Chrome()
              self.driver.maximize_window()
              self.driver.implicitly_wait(10)
              self.driver.get(BASE_URL)
          else:
              self.driver = driver
          self.wait = WebDriverWait( self.driver,10)
      def converter(self,target):
          method, value = target.split("=", 1)
          return method.strip(),value.strip()
      def locate_element(self,target):#target:id="kw",xpath="/erw"
          method,value = self.converter(target)
          # return  self.driver.find_element(method,value)
          return self.wait.until(EC.presence_of_element_located((method,value)))
      #向一个指定的文本框，输入值 target是文本框的元素定位方式(定位方式，定位值)
      def input(self,target,input_value):
          element = self.locate_element(target)
          element.send_keys(input_value)
      def click_button(self,target):
          print('target',target)
          self.locate_element(target).click()
      def get_text(self,target):
          print('get_text_target',target)
          return self.locate_element(target).text
      def get_attr(self,target,attr_name):
          print('get_attr',target,attr_name)
          return self.locate_element(target).get_attribute(attr_name)
      def enterIframe(self,iframe):
          self.driver.switch_to.frame(iframe)
      def switch_toDefault_content(self):
          self.driver.switch_to.default_content()
      def select_target_Value(self,target,value):
          print('target',target)
          roleSelect = Select(self.locate_element(target))
          roleSelect.select_by_value(value)
          #Select(self.driver.locate_element(target).select_by_value(value))
      def excute_js(self,content):
          self.driver.execute_script(content)
      def uploadfile(self,path):
          self.driver.find_element(By.ID,'file-upload').send_keys('/path/to/file.txt')